package com.yuanlrc.hotel.RoomReserve.controller;

import java.util.List;

import com.yuanlrc.hotel.account.domain.Account;
import com.yuanlrc.hotel.account.service.IAccountService;
import com.yuanlrc.hotel.room.service.IRoomService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yuanlrc.common.annotation.Log;
import com.yuanlrc.common.enums.BusinessType;
import com.yuanlrc.hotel.RoomReserve.domain.RoomReserve;
import com.yuanlrc.hotel.RoomReserve.service.IRoomReserveService;
import com.yuanlrc.common.core.controller.BaseController;
import com.yuanlrc.common.core.domain.AjaxResult;
import com.yuanlrc.common.utils.poi.ExcelUtil;
import com.yuanlrc.common.core.page.TableDataInfo;

/**
 * 预约管理Controller
 * 
 * @author yuanlrc
 * @date 2020-11-19
 */
@Controller
@RequestMapping("/hotel/RoomReserve")
public class RoomReserveController extends BaseController
{
    private String prefix = "hotel/RoomReserve";

    @Autowired
    private IRoomReserveService roomReserveService;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IAccountService accountService;

    @RequiresPermissions("hotel:RoomReserve:view")
    @GetMapping()
    public String RoomReserve()
    {
        return prefix + "/RoomReserve";
    }

    /**
     * 查询预约管理列表
     */
    @RequiresPermissions("hotel:RoomReserve:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RoomReserve roomReserve)
    {
        startPage();
        List<RoomReserve> list = roomReserveService.selectRoomReserveList(roomReserve);
        for (RoomReserve item : list) {
            Account account = accountService.selectAccountById(item.getAccountId());
            item.setAccount(account);
            item.setRoom(roomService.selectRoomById(item.getRoomId().intValue()));
        }
        return getDataTable(list);
    }

    /**
     * 导出预约管理列表
     */
    @RequiresPermissions("hotel:RoomReserve:export")
    @Log(title = "预约管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RoomReserve roomReserve)
    {
        List<RoomReserve> list = roomReserveService.selectRoomReserveList(roomReserve);
        ExcelUtil<RoomReserve> util = new ExcelUtil<RoomReserve>(RoomReserve.class);
        return util.exportExcel(list, "RoomReserve");
    }

    /**
     * 新增预约管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存预约管理
     */
    @RequiresPermissions("hotel:RoomReserve:add")
    @Log(title = "预约管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RoomReserve roomReserve)
    {
        return toAjax(roomReserveService.insertRoomReserve(roomReserve));
    }

    /**
     * 修改预约管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RoomReserve roomReserve = roomReserveService.selectRoomReserveById(id);
        mmap.put("roomReserve", roomReserve);
        return prefix + "/edit";
    }

    /**
     * 修改保存预约管理
     */
    @RequiresPermissions("hotel:RoomReserve:edit")
    @Log(title = "预约管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RoomReserve roomReserve)
    {
        return toAjax(roomReserveService.updateRoomReserve(roomReserve));
    }

    /**
     * 删除预约管理
     */
    @RequiresPermissions("hotel:RoomReserve:remove")
    @Log(title = "预约管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try{
            roomReserveService.deleteRoomReserveByIds(ids);
        }catch (Exception e)
        {
            return error("删除失败存在关联数据");
        }
        return toAjax(true);
    }
}
